package com.fs.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.fs.dao.ReceiptDao;
import com.fs.entity.Receipt;


@WebServlet("/receipt.do")
public class RecepitServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req,resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			String type=req.getParameter("type");
			PrintWriter out=resp.getWriter();
			HttpSession session=req.getSession();
			int user_id=1;
			if(session.getAttribute("user_id")!=null){
				user_id=(Integer) session.getAttribute("user_id");
				
			}
			ReceiptDao receiptDao=new ReceiptDao();
			Receipt receipt=requestGetDate(req);
			JSONObject json=new JSONObject();
			/*if("add".equals(type)){
			
				int receipt_id=receiptDao.add(receipt);
				session.setAttribute("receipt_id", receipt_id);//把收货id存在session中
				Receipt receiptInfo=receiptDao.queryreceipt_id(receipt_id);
				//存基本信息对象到req对象中
				req.setAttribute("receiptInfo", receiptInfo);
				req.getRequestDispatcher("pay.jsp").forward(req, resp);
				
			}
			*/
			if("save".equals(type)){
				receiptDao.save(receipt);//执行添加收货，执行需要返回列表（这个时候需要执行查询操作）
				List<Receipt> receiptlist=receiptDao.getReceiptList(user_id);//这个集合需要返到页面
				try {
					JSONArray jsonarray=JSONArray.fromObject(receiptlist);
					json.put("receiptlist",jsonarray );
					json.put("code", 0);
					json.put("message", "添加成功");
					System.out.println(jsonarray.toString());
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "添加失败");
					e.printStackTrace();
				}
				out.println(json.toString());
			}else if("query".equals(type)){
				
				if(session.getAttribute("user_id")!=null){
					user_id=(Integer)session.getAttribute("user_id");
					
				}
				Receipt rec=receiptDao.getReceiptById(user_id);
				req.setAttribute("receipt", receipt);
				req.getRequestDispatcher("pay.jsp").forward(req, resp);
				
			}else if("querylist".equals(type)){
			
				if(session.getAttribute("user_id")!=null){
					user_id=(Integer)session.getAttribute("user_id");
				}
				List<Receipt> receiptlist=receiptDao.getReceiptList(user_id);//这个集合需要返到页面
				JSONArray jsonarray=JSONArray.fromObject(receiptlist);
				json.put("receiptlist",jsonarray );
				json.put("code", 0);
				json.put("message", "查询成功");
				out.println(json.toString());
				
			}else if("delete".equals(type)){
				System.out.println("hahaha");
				String receipt_id=req.getParameter("receipt_id");
				System.out.println(receipt_id);
				try {
					receiptDao.delete(Integer.parseInt(receipt_id));
					json.put("code","0");
					json.put("message", "删除成功");
				} catch (Exception e) {
					json.put("code", 1);
					json.put("message", "删除失败");
					e.printStackTrace();
					System.out.println("删除失败");
				}
			out.println(json.toString());
		}/*else if("order".equals()){
			String id = req.getParameter("id");
			///
			req.setAttribute("asd", arg1);
			req.getRequestDispatcher("success.jsp").forward(req, resp);
		}
	*/
			
			
	}
	private Receipt requestGetDate(HttpServletRequest req){
		HttpSession session=req.getSession();
//获取数据的封装
		String receipt_name=req.getParameter("receipt_name");//收货人姓名
		 String receipt_phone=req.getParameter("receipt_phone");//收货人电话
		 String receipt_addr=req.getParameter("receipt_addr");//收货人电话
		 int user_id=1;
			if(session.getAttribute("user_id")!=null){
				user_id=(Integer) session.getAttribute("user_id");
			}
		 Receipt Receipt=new Receipt(user_id, receipt_name,
					receipt_phone,receipt_addr);
		 Receipt.setUser_id(user_id);
		return Receipt;
	}
	
	private Receipt getRequestData(HttpServletRequest req){
		String receipt_name=req.getParameter("receipt_name");//收货人姓名
		 String receipt_phone=req.getParameter("receipt_phone");//收货人电话
		 String receipt_addr=req.getParameter("receipt_addr");//收货人电话
		 HttpSession session = req.getSession();
		 int user_id=1;
		 if(session.getAttribute("user_id")!=null){
				user_id=(Integer) session.getAttribute("user_id");
			}
		 Receipt receipt=new Receipt();
		 receipt.setUser_id(user_id);
		 receipt.setReceipt_name(receipt_name);
		 receipt.setReceipt_phone(receipt_phone);
		 receipt.setReceipt_addr(receipt_addr);
		 return receipt;
	}


}
